package com.study.modules.hutoolStudy;

import cn.hutool.core.util.XmlUtil;
import cn.hutool.setting.yaml.YamlUtil;
import com.alibaba.fastjson.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import java.io.File;

public class HutoolReadFileTest {

    public static void main(String[] args) {
        testReadYml();
        testReadXml();
    }


    /**
     * 测试读取yml文件
     */
    public static void testReadYml() {
        // 读取相对路径
        JSONObject jsonObject = YamlUtil.loadByPath("application.yml", JSONObject.class);
        JSONObject serverObj = jsonObject.getObject("server", JSONObject.class);
        System.out.println("server.port：" + serverObj.getInteger("port"));
        System.out.println("server.servlet.context-path：" + serverObj.getObject("servlet", JSONObject.class).getString("context-path"));
    }

    /**
     * 测试读取Xml文件
     */
    public static void testReadXml() {
        Document document = XmlUtil.readXML(new File("E:\\ITsoftware\\project\\study\\springboot-shiro\\src\\main\\resources\\rebel.xml"));
        NodeList application = document.getElementsByTagName("application");
        System.out.println(application.item(0).getChildNodes().item(1).getNodeName());
        System.out.println(application.item(0).getChildNodes().item(1).getChildNodes().item(0).getNodeValue());
        System.out.println(application.item(0).getChildNodes().item(3).getNodeName());
        System.out.println(application.item(0).getChildNodes().item(3).getChildNodes().item(1).getNodeName());
        System.out.println(application.item(0).getChildNodes().item(3).getChildNodes().item(1).getAttributes().getNamedItem("name").getNodeValue());
        System.out.println(application.item(0).getChildNodes().item(3).getChildNodes().item(1).getChildNodes().item(0).getNodeValue());
    }
}
